<?php

namespace Driver\Controller;

use Driver\Controller\BaseController;

class OrderController extends BaseController {

    public function _initialize() {
        parent::_initialize();
        if (!$this->driver_id) {
            $this->error('请先登录');
        }
    }

    public function orderList() {
        $order_status = $this->getParam('order_status', -1, 'int+');
        $page = $this->getParam('page', 1, 'int+');
        if ($order_status != -1) {
            $complete_status_arr = [31,32,40,50];
            if(in_array($order_status, $complete_status_arr)){
                $where['order_status'] = array('in',$complete_status_arr);
            }else{
                $where['order_status'] = $order_status;
            }
        }else{
            $where['order_status'] = array('in',[20,30,50]);
        }
        $where['o.driver_id'] = $this->driver_id;
        $orderList = M()->table('ot_order o')->field('o.*,p.port_name,d.dock_name,d.dock_price,dr.driver_phone')
                ->join('LEFT JOIN ot_dock as d ON d.dock_id = o.dock_id')
                ->join('LEFT JOIN ot_port as p ON p.port_id = o.port_id')
                ->join('LEFT JOIN ot_driver as dr ON dr.driver_id = o.driver_id')
                ->where($where)
                ->order('o.order_id DESC')
                ->page($page)
                ->limit(10)
                ->select();
//        if ($orderList) {
//            $OrderAddress = M('order_address');
//            foreach ($orderList as &$val) {
//                $orderAddressInfo = $OrderAddress->where(array('order_id' => $val['order_id'], 'address_type' => 2))->order('order_address_id ASC')->select();
//                $val['shipment_address'] = array();
//                if(!empty($orderAddressInfo)){
//                    foreach($orderAddressInfo as $vv){
//                        $arr_shipment_address[] = json_decode($vv['json_content'], TRUE);
//                    }
//                }
//                $val['shipment_address'] = $arr_shipment_address;
//            }
//            unset($val);
//        }
        if ($orderList) {
            $OrderAddress = M('order_address');
            $LoadArea = M('load_area');
            $Boxman = M('boxman');
            foreach ($orderList as &$val) {
                $orderAddressInfo = $OrderAddress->where(array('order_id' => $val['order_id'], 'address_type' => 2))->order('order_address_id ASC')->select();
                $val['shipment_address'] = array();
                $arr_shipment_address = array();
                if(!empty($orderAddressInfo)){
                    foreach($orderAddressInfo as $vv){
                        $arr_content = json_decode($vv['json_content'], TRUE);
                        $loadArea = $LoadArea->where(array('loadarea_id'=>$arr_content['loadarea_id']))->find();
                        $arr_content['province'] = $loadArea['province'];
                        $arr_content['city'] = $loadArea['city'];
                        $arr_content['loadarea_name'] = $loadArea['loadarea_name'];
                        //装箱工
                        $boxmanInfo = $Boxman->where(['boxman_id'=>$arr_content['boxman_id']])->find();
                        $arr_content['boxman_name'] = $boxmanInfo['boxman_name'];
                        $arr_content['boxman_phone'] = $boxmanInfo['boxman_phone'];
                        $arr_shipment_address[] = $arr_content;
                    }
                }
                $val['shipment_address'] = $arr_shipment_address;
            }
            unset($val);
        }
        $this->returnList($orderList);
    }
    
    //司机订单统计数
    public function orderCountList(){
        $driver_id = $this->driver_id;
        $Order = M('Order');
        $return['wait_box'] = $Order->where(array('order_status'=>20,'driver_id'=>$driver_id))->count();
        $return['transport'] = $Order->where(array('order_status'=>30,'driver_id'=>$driver_id))->count();
        $return['completed'] = $Order->where(array('order_status'=>['in',[31,32,40,50]],'driver_id'=>$driver_id))->count();
        $this->returnList($return);
    }

    /**
     * 订单详情
     * 司机端：已进港，待支付（额外费用审核状态：审核中、被拒绝）对应 企业端同一个订单的：进行中。        
     * 司机端：已进港，待支付（额外费用审核状态：审核通过）对应 企业端同一个订单的：已进港，待支付。
     */
    public function orderDetail() {
        $order_id = $this->getParam('order_id', 0, 'int+', TRUE);

        $where['o.order_id'] = $order_id;
        $orderInfo = M()->table('ot_order o')->field('o.*,p.port_name,d.dock_name,d.dock_price,dw.weight_desc,y.yard_name,y.yard_price,dr.driver_name,dr.driver_phone')
                ->join('LEFT JOIN ot_dock as d ON d.dock_id = o.dock_id')
                ->join('LEFT JOIN ot_port as p ON p.port_id = d.port_id')
                ->join('LEFT JOIN ot_dock_weight as dw ON dw.port_id = d.port_id')
                ->join('LEFT JOIN ot_yard as y ON y.yard_id = o.yard_id')
                ->join('LEFT JOIN ot_driver as dr ON dr.driver_id = o.driver_id')
                ->where($where)
                ->find();
        
        if ($orderInfo) {
            $OrderAddress = M('order_address');
            $LoadArea = M('load_area');
            $Boxman = M('boxman');
            
            $orderAddressList = $OrderAddress->where(array('order_id' => $orderInfo['order_id']))->order('order_address_id ASC')->select();
            if ($orderAddressList) {
                $orderInfo['box_address'] = array();
                $orderInfo['shipment_address'] = array();
                foreach ($orderAddressList as $val) {
                    if ($val['address_type'] == 1) {
                        $orderInfo['box_address'][] = json_decode($val['json_content'], TRUE);
                    }
                    
                    if ($val['address_type'] == 2) {
                        $arr_content = json_decode($val['json_content'], TRUE);
                        $loadArea = $LoadArea->where(array('loadarea_id'=>$arr_content['loadarea_id']))->find();
                        $arr_content['province'] = $loadArea['province'];
                        $arr_content['city'] = $loadArea['city'];
                        $arr_content['loadarea_name'] = $loadArea['loadarea_name'];
                        //装箱工
                        $boxmanInfo = $Boxman->where(['boxman_id'=>$arr_content['boxman_id']])->find();
                        $arr_content['boxman_id'] = $boxmanInfo['boxman_id'];
                        $arr_content['boxman_name'] = $boxmanInfo['boxman_name'];
                        $arr_content['boxman_phone'] = $boxmanInfo['boxman_phone'];
                        $arr_shipment_address[] = $arr_content;
                    }
                }
                $orderInfo['shipment_address'] = $arr_shipment_address;
            }
            //物流信息
            $orderInfo['logistics'] = M('order_logistics')->where(['order_id' => $order_id])->order('order_logistics_id DESC')->select();
            
            
            //装箱图列表
            if(!empty($orderInfo['box_img'])){
                $arr_box_img = json_decode($orderInfo['box_img'], TRUE);
                foreach($arr_box_img as &$vv){
                    $vv = C('SITE_URL').$vv;
                }
                unset($vv);
            }
            $orderInfo['box_img'] = $arr_box_img;
            
            //其他费用图片
            if(!empty($orderInfo['other_price_img'])){
                $arr_other_price_img = json_decode($orderInfo['other_price_img'], TRUE);
                foreach($arr_other_price_img as &$vv){
                    $vv = C('SITE_URL').$vv;
                }
                unset($vv);
            }
            $orderInfo['other_price_img'] = $arr_other_price_img;
            $orderInfo['other_price_total'] = floatval($orderInfo['dock_price']+$orderInfo['weight_price']+$orderInfo['yard_price']+$orderInfo['other_price']+$orderInfo['admin_price']);
        }
        $this->returnList($orderInfo);
    }
    
    
    //完成提箱
    public function completeBox() {
        $order_id = $this->getParam('order_id', 0, 'int+', TRUE);
        $box_no = $this->getParam('box_no');
        $close_no = $this->getParam('close_no');
        $box_img_json_list = $this->getParam('box_img_json_list');

        $saveData = [];
        !empty($box_no) && $saveData['box_no'] = $box_no;
        !empty($close_no) && $saveData['close_no'] = $close_no;
        $saveData['close_no'] = $close_no;
        $saveData['box_create_time'] = date('Y-m-d H:i:s');
        //$this->uploadFileBase64($base64Data);
        if ($box_img_json_list) {
            foreach (explode(',', $box_img_json_list) as $val) {
                $imgList[] = $this->uploadFileBase64($val, 'Driver');
            }
        }
        D()->startTrans();
        !empty($imgList) && $saveData['box_img'] = json_encode($imgList);
        $saveData['order_status'] = 30;
        $res = M('order')->where(['order_id'=>$order_id])->save($saveData);
        
        //更新装箱工状态未待接车
        M('order_address')->where(['order_id'=>$order_id,'address_type'=>2])->save(['boxman_order_status'=>10]);
        if($res){
            D()->commit();
            $this->success();
        }else{
            D()->rollback();
            $this->error();
        }
    }
    
    //确认进港
    public function confirmInPort() {
        $order_id = $this->getParam('order_id', 0, 'int+', TRUE);
//        $prot_id = $this->getParam('prot_id', 0, 'int+', TRUE);
        $dock_id = $this->getParam('dock_id', 0, 'int+');
        $weight_id = $this->getParam('weight_id', 0, 'int+');
        $yard_id = $this->getParam('yard_id', 0, 'int+');
        $other_price = $this->getParam('other_price',0);
        $other_price_desc = $this->getParam('other_price_desc');
        $other_price_img = $this->getParam('other_price_img');
        
        $data['dock_id'] = $dock_id;
        $data['weight_id'] = $weight_id;
        $data['yard_id'] = $yard_id;
        $data['dock_price'] = M('dock')->where(['dock_id'=>$dock_id])->getField('dock_price');
        $data['weight_price'] = M('dock_weight')->where(['weight_id'=>$weight_id])->getField('weight_price');
        $data['yard_price'] = M('yard')->where(['yard_id'=>$yard_id])->getField('yard_price');
        $data['other_price'] = $other_price;
        $data['other_price_desc'] = $other_price_desc;
                
        if ($other_price_img) {
            foreach (explode(',', $other_price_img) as $val) {
                $imgList[] = $this->uploadFileBase64($val, 'Driver');
            }
        }
        if(!empty($imgList)){
            $data['other_price_img'] = json_encode($imgList);
        }
        $data['order_status'] = 31;
        $res = M('order')->where(['order_id'=>$order_id])->save($data);
        if($res){
            $this->success();
        }else{
            $this->error();
        }
    }
    
    //更新司机位置
    public function updateOrderDriverLonLat(){
        $driver_id = $this->driver_id;
        $data['driver_lon'] = $this->getParam('driver_lon', '', 'string', TRUE, '传参司机经度');
        $data['driver_lat'] = $this->getParam('driver_lat', '', 'string', TRUE, '传参司机纬度');
        $data['lon_lat_address'] = $this->getParam('lon_lat_address');
        $res = M('driver')->where(['driver_id'=>$driver_id])->save($data);
        $this->success();
    }

}
